<template>
	<view>
		<musichead title='搜索' :icon='true' :iconblack="true"></musichead>
		
		<view class="container">
			<scroll-view scroll-y="true" >
				
				<view class="index-search">
					<text class="iconfont icon-search"></text>
					<input type="text" placeholder="搜索歌曲" v-model="searchword" @input="handletosuggest" @confirm="handletosearch(searchword)" >
					<text v-show="searchType!=1" class="iconfont icon-close-bold" @tap="handletoclose()"></text>
				</view>
				<block  v-if="searchType == 1">
					
					<view class="search-history">
						<view class="search-history-head">
							<text>历史记录</text>
							<text class="iconfont icon-ashbin" @tap="handletoclean()"></text>
						</view>
						<view class="search-histoyr-list">
							<view v-for="(item,index) in searchhistory" :key="index" @tap="handletoword(item)">{{item}}</view>
						</view>
					</view>
					<view class="search-hot">
						<view class="search-hot-head">热搜榜</view>
					<!-- 	<view class="search-hot-item">
							<view class="search-hot-top">1</view>
							<view class="search-hot-word">
								<view>
									少年<image src="" mode=""></image>
								</view>
								<view>“少年”这个词实在是太美了</view>
							</view>
							<text class="search-hot-count">232323</text>
						</view> -->
						
						<view class="search-hot-item" v-for="(item,index) in searchhot" :key=index @tap="handletoword(item.searchWord)">
							<view class="search-hot-top">{{index+1}}</view>
							<view class="search-hot-word">
								<view>
									{{item.searchWord}}<image :src="item.iconUrl" mode=""></image>
								</view>
								<view>{{item.content}}</view>
							</view>
							<text class="search-hot-count">{{item.score}}</text>
						</view>
						
								
					</view>
				</block>
				
				<block v-else-if="searchType == 2">
					<view class="search-result">
						<view class="search-result-item" v-for="(item,index) in searchlist" :key = "index" @tap="handletodetail(item.id)">
							<view class="search-result-item-word">
								<view>{{item.name}}</view>
								<view>{{item.artists[0].name}} - {{item.album.name}}</view>
							</view>
							<text class="iconfont icon-bofang"></text>
						</view>
						
					</view>
				</block>
				
				<block v-else-if="searchType == 3">
					<view class="search-suggest">
						<view class="search-suggest-head">搜索“{{searchword}}”</view>
						<view class="search-suggest-item" v-for="(item,index) in searchsuggest" :key = "index" @tap="handletoword(item.name)">
							<text class="iconfont icon-search"></text>{{item.name}}
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import musichead from"../../components/musichead/musichead.vue"
	import {searchhot,searchword,searchsuggest} from "../../common/api.js"
	export default {
		data() {
			return {
				searchhot:[],
				searchword:'',
				searchhistory:[],
				searchType:1,
				searchlist:[],
				searchsuggest:[] 
			}
		},
		methods: {
			handletoword(word){
				this.searchword = word
				this.handletosearch(word)
			},
			handletosearch(word){
				console.log(word);
				this.searchhistory.unshift(word)
				this.searchhistory = [...new Set(this.searchhistory)]
				if(this.searchhistory.length>10) 
					this.searchhistory.length = 10
				uni.setStorage({
					key:"searchhistory",
					data:this.searchhistory
				})
				this.getsearchlist(word)
			},
			handletoclean(){
				this.searchhistory = []
				uni.removeStorage({
					key:"searchhistory",
					success() {
						this.searchhistory = []
					}
				})
			},
			
			getsearchlist(word){  
				searchword(word).then(res=>{
					this.searchlist = res[1].data.result.songs
					this.searchType = 2
				})
			},
			
			handletoclose(){
				this.searchword = ''
				this.searchType = 1
			},
			
			handletodetail(id){
				uni.navigateTo({
					url:"../detail/detail?id="+id,
					
				}) 
			},
			handletosuggest(ev){
				let value = ev.detail.value
				if(!value){
					this.searchType = 1
					return
				}
				searchsuggest(value).then(res=>{
					this.searchsuggest  = res[1].data.result.songs
					console.log(this.searchsuggest);
					this.searchType = 3
				})
			}
		},
		onLoad() {
			searchhot().then(res=>{
				this.searchhot = res[1].data.data
			})
			
			uni.getStorage({
				key:"searchhistory",
				success:(res)=>{
					this.searchhistory = res.data
				}
			})
		}
	}
</script>

<style scoped>
	.index-search{display: flex; height: 70rpx; margin: 70rpx 30rpx 30rpx 30rpx;background: #f7f7f7;border-radius: 50rpx; align-items: center;}
	.index-search text{font-size: 26rpx; margin-right: 26rpx;margin-left: 28rpx;}
	.index-search input{font-size: 28rpx; flex: 1;}
	
	.search-history{margin: 0 30rpx 50rpx 30rpx;font-size: 26rpx;}
	.search-history-head{display: flex;justify-content: space-between;margin-bottom: 36rpx;}
	.search-histoyr-list{display: flex;flex-wrap: wrap;}
	.search-histoyr-list view{padding: 16rpx 28rpx;border-radius: 40rpx;margin-bottom: 30rpx;margin-right: 30rpx;background: #F7F7F7;}
	
	.search-hot{margin: 0 30rpx;font-size: 26rpx;}
	.search-hot-head{margin-bottom: 36rpx;}
	.search-hot-item{display: flex;align-items: center;margin-bottom: 58rpx;}
	.search-hot-top{color: #fb2222;width: 60rpx;margin-left: 8rpx;}
	.search-hot-word{flex: 1;}
	.search-hot-word view:nth-child(1){font-size: 30rpx;color: black;}
	.search-hot-word view:nth-child(2){font-size: 24rpx;color: #878787;}
	.search-hot-word image{width: 48rpx;height: 22rpx;}
	.search-hot-count{color: #878787;}
	
	.search-result{border-top: 2rpx #e4e4e4 solid; padding: 30rpx; }
	.search-result-item{display: flex; justify-content: space-between; align-items: center;padding-bottom: 30rpx;margin-bottom: 30rpx;
		border-bottom: 2rpx #e4e4e4 solid;
	}
	.search-result-item text{font-size: 50rpx;}
	.search-result-item-word{}
	.search-result-item-word view:nth-child(1){font-size: 28rpx; color: #235790; margin-bottom: 12rpx;}
	.search-result-item-word view:nth-child(2){font-size: 22rpx; color: #898989;}
	
	.search-suggest{border-top: 2rpx #e4e4e4 solid; padding: 30rpx; font-size: 26rpx;}
	.search-suggest-head{color: #4574a5;margin-bottom: 74rpx;}
	.search-suggest-item{color: #5d5d5d;margin-bottom: 74rpx;}
	.search-suggest-item text{color: #bdbdbd;margin-right: 28rpx;position: relative;top: 2rpx;}
</style>
